package com.plian.system.controller.pm.annualdonationrecord;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.plian.Tools.base.BaseController;
import com.plian.system.entity.pm.annualdonationrecord.AnnualDonationRecord;
import com.plian.system.entity.pm.annualdonationrecordmain.AnnualDonationRecordMain;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.mybatis.search.SearchEntity;
import com.plian.system.mybatis.search.SearchParamEntiy;
import com.plian.system.mybatis.support.Condition;
import com.plian.system.mybatis.support.Query;
import com.plian.system.service.pm.annualdonationrecord.IAnnualDonationRecordService;
import com.plian.system.service.pm.annualdonationrecordmain.IAnnualDonationRecordMainService;
import com.plian.system.vo.pm.annualdonationrecord.AnnualDonationRecordVO;
import com.plian.system.wrapper.pm.annualdonationrecord.AnnualDonationRecordWrapper;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 企业年度捐赠计划备案子表 控制器
 *
 * @author panqb
 * @since 2022-04-20
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/api/annualdonationrecord")
@Api(value = "企业年度捐赠计划备案明细", tags = "企业年度捐赠计划备案明细")
public class AnnualDonationRecordController extends BaseController {
    IAnnualDonationRecordService annualDonationRecordService;

    IAnnualDonationRecordMainService annualDonationRecordMainService;

    AnnualDonationRecordWrapper annualDonationRecordWrapper;

    /**
     * 捐赠计划项目备案报表
     * @param query
     * @return
     */
    @PostMapping("report")
    public ListResult<AnnualDonationRecordVO> list(@RequestBody Query query){
        ListResult<AnnualDonationRecordVO> listResult=new ListResult<>(CommonCode.SUCCESS);
        List<SearchEntity> entityList = query.getSearch();
        if (query.getAscs()==null&&query.getDescs()==null){
            query.setDescs("create_time");
        }
        QueryWrapper<AnnualDonationRecordMain> queryWrapper = SearchParamEntiy.getQueryWrapper(
                Condition.getQueryWrapper(new AnnualDonationRecordMain()), entityList
        );
//        if (!Optional.ofNullable(query.getFlag()).isPresent() || query.getFlag() != 1) {
//            queryWrapper.eq("create_company_id", TokenUtil.getTokenOrgId());
//        }
        List<AnnualDonationRecordMain> list=annualDonationRecordMainService.list(queryWrapper);
        if(list.size()==0){
            return listResult;
        }
        List<Long> mainIds=list.stream().map(AnnualDonationRecordMain::getId).collect(Collectors.toList());
        Map<Long,String> yearMap=list.stream().collect(Collectors.toMap(AnnualDonationRecordMain::getId,AnnualDonationRecordMain::getYear ));
        QueryWrapper<AnnualDonationRecord> recordQueryWrapper=new QueryWrapper<>();
        recordQueryWrapper.in("parent_id",mainIds);
        List<AnnualDonationRecord> recordList=annualDonationRecordService.list(recordQueryWrapper);
        List<AnnualDonationRecordVO> recordVOList=annualDonationRecordWrapper.entityToVO(recordList);
        recordVOList.forEach(record->{
            Long parentId=record.getParentId();
            if(yearMap.containsKey(parentId)){
                record.setYear(yearMap.get(parentId));
            }
        });
        listResult.setList(recordVOList);
        listResult.setTotal(recordVOList.size());
        return listResult;
    }
}
